﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Services;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.SmsAdmin.Controllers
{
    [CustomAuthorize(Roles = "ROLE_SMSADMIN")]
    public class UserController : Controller
    {
        //
        // GET: /Admin/User/

        private IUserRepository _userRepository;
        private IDropDownService _dropDownService;
        private IAuditTrailRepository _auditTrailRepository;
        CodersEncrypt passEncrypt = new CodersEncrypt();

        public UserController(IUserRepository userRepository, IDropDownService dropDownService, IAuditTrailRepository auditTrailRepository)
        {
            _userRepository = userRepository;
            _dropDownService = dropDownService;
            _auditTrailRepository = auditTrailRepository;
        }

        private void Bind()
        {
            ViewBag.UserTypeList = _dropDownService.EnumToList<UserType>();
            ViewBag.UserGroupList = _dropDownService.GetUserGroup();
        }
        public ActionResult Index()
        {
            return View(_userRepository.GetAll());
        }
        public ActionResult Create()
        {
            Bind();
            return View(new User());
        }

        [HttpPost]
        public ActionResult Create(User model)
        {
            try
            {
                model.Password = passEncrypt.EncryptThings("1234");
                model.ConfirmPassword = passEncrypt.EncryptThings("1234");
               _userRepository.Save(model);
               Saveaudit("Added User", HttpContext.User.Identity.Name, ActionType.Added);
            }catch(DomainValidationException dve)
            {
                Bind();
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch(Exception ex)
            {
                Bind();
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            Bind();
            var model = _userRepository.GetById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(User model)
        {
            try
            {
                _userRepository.Save(model);
                Saveaudit("Edited User", HttpContext.User.Identity.Name, ActionType.Updated);
            }
            catch (DomainValidationException dve)
            {
                Bind();
                ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                return View(model);
            }
            catch (Exception ex)
            {
                Bind();
                ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                return View(model);
            }
            return RedirectToAction("Index");
        }


        public ActionResult Delete(int id)
        {
            _userRepository.Delete(id);
            return RedirectToAction("index");
        }
        public ActionResult Details(int id)
        {
            User user = new User();
            user = _userRepository.GetById(id);
            return View(user);
        }
         
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";
                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            
            }
        }
        public void Saveaudit(string description, string username, ActionType actionType)
        {
            //  private IUserRepository _userRepository;
            //private IAuditTrailRepository _auditTrailRepository;
            // Saveaudit("Changed Password", HttpContext.User.Identity.Name, ActionType.Updated);
            try
            {
                User user = _userRepository.GetByUsername(username);
                AuditTrail auditTrail = new AuditTrail();
                auditTrail.Description = description;
                auditTrail.UserId = user.Id;
                auditTrail.ActionTypeId = Convert.ToInt32(actionType);
                _auditTrailRepository.Save(auditTrail);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
